package cc.javaee.bbs.request;

import cc.javaee.bbs.tool.Connection;
import cc.javaee.bbs.tool.PropertiesUtil;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author xdw
 */
public class KoboroRequest {

    private static String WINNERS_URL = "/appInteface/getInfoByCode.htm";
    private static String ALL_WALK_URL = "/appInteface/getPersonalInfoByCode.htm";
    private static String RANKING_URL = "/appInteface/getPersonalFirstThreeInfoByCode.htm";
    private static String CUSTOMER_URL = "/appInteface/cust-getCustByArchivalNumber.htm";

    // 获取合格的人
    public static Map getWinners(List<String> archivalNumberList, Date startTime, Date endTime, int walkCount, int type) {
        String url = PropertiesUtil.getProperties("koboro.server") + WINNERS_URL;

        String archivalNumbers = String.join(",", archivalNumberList);
        Map map = Connection.newPOST()
                .setParam("archivalNumbers", archivalNumbers)
                .setParam("startTime", String.format("%tF", startTime))
                .setParam("endTime", String.format("%tF", endTime))
                .setParam("walkCount", walkCount + "")
                .setParam("type", type + "")
                .send(url, HashMap.class);
        return map;
    }

    // 获取总步数
    public static Map getAllWalk(String archivalNumber, Date startTime, Date endTime) {
        String url = PropertiesUtil.getProperties("koboro.server") + ALL_WALK_URL;

        String sout = Connection.newPOST()
                .setParam("archivalNumber", archivalNumber)
                .setParam("startTime", String.format("%tF", startTime))
                .setParam("endTime", String.format("%tF", endTime))
                .send(url, String.class);
        System.out.println(sout);

        Map map = Connection.newPOST()
                .setParam("archivalNumber", archivalNumber)
                .setParam("startTime", String.format("%tF", startTime))
                .setParam("endTime", String.format("%tF", endTime))
                .send(url, HashMap.class);
        return map;
    }

    // 获取排名
    public static Map getRanking(List<String> archivalNumberList, Date startTime, Date endTime) {
        String url = PropertiesUtil.getProperties("koboro.server") + RANKING_URL;

        String archivalNumbers = String.join(",", archivalNumberList);
        Map map = Connection.newPOST()
                .setParam("archivalNumber", archivalNumbers)
                .setParam("startTime", String.format("%tF", startTime))
                .setParam("endTime", String.format("%tF", endTime))
                .send(url, HashMap.class);

        List<Map> list = (List) map.get("data");
        if (list == null) {
            list = new ArrayList();
        }
        Map result = new HashMap();
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(result::putAll);
        }
        return result;

    }

    public static Map getCustomer(String archivalNumber) {
        String url = PropertiesUtil.getProperties("koboro.server") + CUSTOMER_URL;

        Map map = Connection.newPOST()
                .setParam("archivalNumber", archivalNumber)
                .send(url, HashMap.class);
        Map result = (Map) map.get("data");
        if (result == null) {
            result = new HashMap();
        }
        return result;
    }


    public static void main(String[] args) throws InterruptedException {
        List<String> list = new ArrayList<>();
        list.add("20161220000001");
        list.add("20161220000002");
        list.add("20161220000003");
        list.add("20161220000004");
        list.add("20161220000005");
        list.add("20161220000006");
        list.add("20161220000007");
        list.add("20161220000008");
        list.add("20161220000009");
        list.add("20161220000010");
        list.add("20161220000011");
        list.add("20161220000012");
        list.add("20161220000013");
        list.add("20161220000014");
        list.add("20161220000015");
        list.add("20161220000016");
        list.add("20161220000017");
        list.add("20161220000018");
        list.add("20161221000001");
        list.add("20161221000002");
        list.add("20161221000003");
        list.add("20161221000004");
        list.add("20170726001386");
        list.add("20170726001247");
        list.add("20170726001735");
        list.add("20170726002420");
        list.add("20170726001130");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1);

        Calendar calendar2 = Calendar.getInstance();
        calendar2.add(Calendar.DAY_OF_MONTH, -1);

        Map map = getWinners(list, new Date(), new Date(), 1000, 2);
        Map map2 = getAllWalk("20161220000017", calendar.getTime(), calendar2.getTime());
        Map map3 = getCustomer("20161220000017");
        Map map4 = getRanking(list, calendar.getTime(), calendar2.getTime());

        System.out.println(map);
        System.out.println(map2);
        System.out.println(map3);
        System.out.println(map4);
    }


}
